package com.upoint.sellorderservice.controller;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.upoint.sellorderservice.message.CloudStreamClient;
import com.upoint.sellorderservice.message.CloudStreamMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Random;

/**
 * @author jsbintask@gmail.com
 * @date 2019/2/21 14:38
 */
@RestController
@RequestMapping
public class CloudStreamController {
    @Autowired
    private CloudStreamClient cloudStreamClient;
    @Autowired
    private ObjectMapper objectMapper;

    @GetMapping("add-msg")
    public void addMsg() throws Exception{
        System.out.println("send msg...");
        Map<String, String> payload = new HashMap<>();
        payload.put("data", "hello cloud stream: " + new Date());


        CloudStreamMessage cloudStreamMessage = new CloudStreamMessage();
        cloudStreamMessage.setId(new Random().nextInt());
        cloudStreamMessage.setData(payload);

        cloudStreamClient.input().send(MessageBuilder.withPayload(objectMapper.writeValueAsString(cloudStreamMessage)).build());
    }
}
